package com.example.module_user

import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.Toast
import androidx.lifecycle.lifecycleScope
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.example.lib_base.BaseActivity
import com.example.module_user.databinding.ActivityRegisterBinding
import com.example.module_user.intent.UserIntent
import com.example.module_user.state.UserState
import com.example.module_user.viewmodel.UserViewModel
import kotlinx.coroutines.flow.collect
import kotlinx.coroutines.launch

@Route(path = "/sp/jump")
class RegisterActivity : BaseActivity<ActivityRegisterBinding>() {
    private val viewModel by lazy{ UserViewModel() }
    override fun initData() {
      lifecycleScope.launch {
          viewModel.observerState.collect{
              when(it){
                  is UserState.RegisterSuccess->{
                      Toast.makeText(this@RegisterActivity, "注册成功", Toast.LENGTH_SHORT).show()
                  }
                  else->{

                  }
              }
          }
      }

    }

    override fun initView() {
       binding.jumpLogin.setOnClickListener {
           val intent=Intent(this@RegisterActivity,UserActivity::class.java)
           startActivity(intent)
       }
        if(binding.registerPassword.text.length<8){
            binding.registerPassword.error = "密码长度不能小于八位"
        }

        binding.registerButton.setOnClickListener{
            if(binding.registerPassword.text.toString() == binding.registerRPassword.text.toString()){
                lifecycleScope.launch {
                    viewModel.channel.send(UserIntent.Register(binding.registerPassword.text.toString(),binding.registerUsername.text.toString()))
                }
            }else{
                Toast.makeText(this, "密码不匹配", Toast.LENGTH_SHORT).show()
            }

        }
    }

    override fun getLayoutId(): Int =R.layout.activity_register

}